<template>
	<view class="content">
		<view class="search-heder">
			<view class="search-tabs">
				<navigator url="../pages/index/index.vue" open-type="navigateBack" :delta="1">
					<u-icon name="arrow-left" color="#000" size="22"></u-icon>
				</navigator>
				<view class="search-u"><u-search bgColor="#fff" :showAction="false" placeholder="请输入关键字 如:菜品名称" v-model="keyword"></u-search></view>
				<text @click="searchbtn">搜索</text>
			</view>
			<view class="search-img">
				<image src="../static/search-bing.png" mode="widthFix"></image>
				<text>没有什么比吃饭更重要~</text>
			</view>
		</view>
		<view class="search-content">
			<!-- 历史搜索 -->
			<view class="search-history" v-if="searchlist.length !== 0">
				<view class="history-top">
					<text>历史搜索</text>
					<text class="iconfont icon-shanchu" @click="show = true"></text>
				</view>
				<view class="history-content active">
					<view class="history-item" v-for="(item,index) in searchlist" :key="index" @click="keyword = item">{{item}}</view>
				</view>
			</view>
			<!-- 查询结果 -->
			<view class="search-end" v-if="load">
				<view class="history-top">
					<text>查询结果</text>
				</view>
				<view class="searchcontent">
					<view class="product">
						<CommoDity ref="delson" :productlist="finelist"></CommoDity>
					</view>
				</view>
			</view>
			<!-- 精品推荐 -->
			<view class="search-Boutique" v-else>
				<view class="history-top">
					<text>精品推荐</text>
					<text class="iconfont icon-shuaxin" @click="resh"></text>
				</view>
				<view class="history-content">
					<CommoDity ref="delson" :productlist="finelist"></CommoDity>
				</view>
			</view>
		</view>
		<u-modal :show="show" title="是否删除全部历史记录" :showCancelButton='true' @cancel='show = false' @confirm="searchconfirm" ref="searchmd"
			:asyncClose="true"></u-modal>
	</view>
</template>

<script>
	import CommoDity from '../components/commodity.vue'
	import {getproduct} from '@/api/commodity.js'
	export default {
		components: {
			CommoDity
		},
		data() {
			return {
				keyword: '',
				searchlist: [],
				show: false,
				load:false,
				finelist:[],
				count:0,
				pages:1
			}
		},
		methods: {
			async searchbtn() {
				this.$refs.delson.remove()
				this.load = true
				this.finelist = []
				this.searchlist.push(this.keyword)
				uni.setStorageSync('searchlist', this.searchlist)
				const res = await getproduct({
					name:this.keyword,
					limit:10,
					page:this.pages
				})
				this.finelist.push(...res.data.list)
				this.keyword = ''
			},
			searchconfirm(){
				uni.removeStorageSync('searchlist')
				this.searchlist = []
				this.show = false
			},
			async findapi(pages){
				const res = await getproduct({
					is_recommend:1,
					limit:10,
					page:pages
				})
				this.finelist = res.data.list
				this.count = res.data.count
			},
			resh(){
				let pages=Math.floor(Math.random() * (Math.floor(this.count/10) - 1)) + 1
				this.findapi(pages)
			}
		},
		onLoad() {
			this.searchlist = uni.getStorageSync('searchlist') || []
			this.findapi(1)
		}
	}
</script>

<style lang="scss">
	.search-heder {
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		box-sizing: border-box;
		padding: 0 40rpx;
		padding-top: 156rpx;
		height: 360rpx;
		background: linear-gradient(180deg, #e6eee3 28.52%, #f2f9ef 93.61%);
		z-index: 999;
		.search-tabs {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.search-u {
				width: 498rpx;
			}
		}

		.search-img {
			position: absolute;
			bottom: -11rpx;
			display: flex;
			align-items: center;
			color: #999;
			font-size: 24rpx;

			image {
				width: 100rpx;
				margin-right: 81rpx;
			}
		}
	}

	.search-content {
		margin-top: 390rpx;
		padding: 0 40rpx;

		.history-top {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 30rpx;
			margin-bottom: 20rpx;
			.icon-shanchu {
				font-weight: bold;
				color: #999;
			}
		}

		.history-content.active {
			display: flex;
			flex-wrap: wrap;
			margin-top: 20rpx;
			font-size: 24rpx;
			.history-item {
				padding: 9rpx 24rpx;
				background-color: #E6E6E6;
				border-radius: 30rpx;
				margin-right: 20rpx;
				margin-top: 18rpx;
			}
		}

		.search-Boutique {
			margin-top: 37rpx;

			.icon-shuaxin {
				color: #999;
			}
		}
		.search-end{
			margin-top: 37rpx;
		}
	}
</style>